package com.ce.pms.customer.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;

import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.customer.dao.IClientDao;
import com.ce.pms.customer.model.ClientDO;
import com.ce.pms.customer.model.ClientRHouseDO;
import com.ce.pms.customer.model.ParticipantDO;

public class ClientDaoImpl extends PmsBaseDaoImpl<ClientDO> implements IClientDao {

	public ClientDO addClient(ClientDO clientDO) {
		// TODO Auto-generated method stub
		this.saveObject(clientDO);
		return clientDO;
	}

	/**
	 *
	 * 功能描述: 根据参与人和客户类型得到对应的客户实体
	 *
	 * @author 付湘辉
	 * Created on:2008-6-11
	 *
	 * 修改原因：
	 * @param participantDO
	 * @param clientType
	 * @return ClientDO 返回结果
	 */
	public ClientDO queryClientByParticipantAndClientType(ParticipantDO participantDO, SysrefcodeDO clientType){
		return this.queryClientByParticipantAndClientType(participantDO.getParticipantId(), clientType.getId());
	}

	/**
	 *
	 * 功能描述: 根据参与人id和客户类型id得到对应的客户实体
	 *
	 * @author 付湘辉
	 * Created on:2008-6-11
	 *
	 * 修改原因：
	 * @param participantDO
	 * @param clientType
	 * @return ClientDO 返回结果
	 */
	public ClientDO queryClientByParticipantAndClientType(Long participantId, Long clientTypeId) {
		ClientDO clientDO = null;

		StringBuffer queryStr = new StringBuffer();
		queryStr.append("SELECT client FROM ClientDO client WHERE client.participant.participantId= ? AND client.clientType.id=? ");

		Query query=super.createQuery(queryStr.toString(), participantId , clientTypeId);
		List list = query.list();
		if(list.size()>0){
			clientDO = (ClientDO)list.get(0);
		}
		return clientDO;
	}

	/**
	 *
	 * 功能描述: 根据参与人id和客户类型CODE得到对应的客户实体
	 *
	 * @author 付湘辉
	 * Created on:2008-6-11
	 *
	 * 修改原因：
	 * @param participantDO
	 * @param clientType
	 * @return ClientDO 返回结果
	 */
	public ClientDO queryClientByParticipantAndCTypeCode(Long participantId, String clientTypeCode) {
		ClientDO clientDO = null;

		StringBuffer queryStr = new StringBuffer();
		queryStr.append("SELECT client FROM ClientDO client WHERE client.participant.participantId= ? AND client.clientType.code=? ");

		Query query=super.createQuery(queryStr.toString(), participantId , clientTypeCode);
		List list = query.list();
		if(list.size()>0){
			clientDO = (ClientDO)list.get(0);
		}
		return clientDO;
	}


	public List<Long> getHouseIdsByClientId(Long clientId) {
		List<Long> retValue = new ArrayList<Long>();
		ClientDO client = this.findById(clientId);
		for(ClientRHouseDO crh : client.getClientRHouses()) {
			retValue.add(crh.getHouse().getKeyword());
		}
		return retValue;

	}

}
